package com.tjkxzx.cfnie.core.service.impl;

import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.tjkxzx.cfnie.core.dao.entity.CfnMachineRoom;
import com.tjkxzx.cfnie.core.dao.entity.CfnProperty;
import com.tjkxzx.cfnie.core.dao.mapper.CfnMachineRoomMapper;
import com.tjkxzx.cfnie.core.dao.mapper.CfnPropertyMapper;
import com.tjkxzx.cfnie.core.service.CfnMachineRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Luc
 * @since 2024/01/08
 */
@Service
@RequiredArgsConstructor
public class CfnMachineRoomServiceImpl extends ServiceImpl<CfnMachineRoomMapper, CfnMachineRoom> implements CfnMachineRoomService {

    private final CfnMachineRoomMapper cfnMachineRoomMapper;

    private List<CfnMachineRoom> cfnMachineRooms = new ArrayList<>();

    @Override
    public void clearTable() {
        cfnMachineRoomMapper.truncate();
    }

    @Override
    public List<CfnMachineRoom> readData(String filePath) {
        ExcelUtil.readBySax(filePath, 0, createRowHandler());
//        if(cfnMachineRooms.size() > 0) {
//            this.saveBatch(cfnMachineRooms);
//        }
        return cfnMachineRooms;
    }

    private CfnMachineRoom buildObject(List<Object> target) throws IllegalAccessException {
        CfnMachineRoom cfnMachineRoom = new CfnMachineRoom();
        Class<CfnMachineRoom> clazz = CfnMachineRoom.class;
        Field[] fields = clazz.getDeclaredFields();
        boolean isNull = true;
        for(int i=2; i<=55; i++) {
            Field field = fields[i];
            field.setAccessible(true);
            if(Objects.isNull(target.get(i-2))) {
                continue;
            }
            isNull = false;
            field.set(cfnMachineRoom, target.get(i-2).toString().trim());
        }
        if(isNull) {
            return null;
        }
        return cfnMachineRoom;
    }

    private RowHandler createRowHandler() {
        return new RowHandler() {

            @Override
            public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {

            }

            @Override
            public void doAfterAllAnalysed() {

            }

            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {
                Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
                if(rowIndex > 0) {
                    try {
                        CfnMachineRoom cfnMachineRoom = buildObject(rowlist);
                        if(!Objects.isNull(cfnMachineRoom)) {
                            cfnMachineRooms.add(cfnMachineRoom);
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }
}
